package cn.bdqn.service.questionBank;

import cn.bdqn.dao.questionBank.IndustryDao;
import cn.bdqn.dto.questionBank.subject.SubjectAccountTypeDTO;
import cn.bdqn.pojo.questionBank.Industry;
import cn.bdqn.pojo.questionBank.OperationRecord;
import cn.bdqn.utils.Json;
import cn.bdqn.vo.questionBank.IndustryAccountType.IndustryAccountTypeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * &#064;Author  sx
 */
@Service("industryService")
public class IndustryService {

    @Autowired
    private IndustryDao industryDao;

    @Autowired
    private OperationRecordService operationRecordService;



    /**
     * 查询所有行业信息和科目信息
     *
     * @return json
     * &#064;Author  sx
     */
    public Json selectIndustryAccountType(){

        List<IndustryAccountTypeVo> industryAccountTypeVoList = industryDao.selectIndustryAccountType();

        //System.out.println(industryAccountTypeVoList.get(3).getIndustryAccountTypePoList());
        return new Json(200,"获取数据成功",industryAccountTypeVoList);
    }

    /**
     * 测试接口
     * @return
     */
    public List<Industry> selectAllIndustry(){
        return industryDao.selectList(null);
    }

    /**
     * 修改行业表
     * @param subjectAccountTypeDTO
     * @param userId
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public Json updateIndustryById(SubjectAccountTypeDTO subjectAccountTypeDTO,Integer userId) throws Exception {
        OperationRecord operationRecord = new OperationRecord();
        //先添加操作表数据
        operationRecord.setUserId(userId);
        operationRecord.setOperationRecordDescribe("修改操作");
        operationRecord.setOperationRecordRemarks(subjectAccountTypeDTO.getOperationRecordRemarks());
        operationRecord.setOperationRecordTime(subjectAccountTypeDTO.getTime());
        //报错抛异常让aop回滚
        if(operationRecordService.insertOperationRecord(operationRecord) == null){
            System.out.println("ymy ");
            throw new Exception("添加操作表失败失败");
        }

        //在更新行业表数据
        Industry industry = new Industry();
        industry.setIndustryId(subjectAccountTypeDTO.getId());
        industry.setIndustryName(subjectAccountTypeDTO.getName());
        industry.setIndustryParentId(subjectAccountTypeDTO.getTypeId());
        industry.setUpdateOperationRecordId(operationRecord.getOperationRecordId());
        industry.setIndustryTree(selectIndustryTree(subjectAccountTypeDTO.getTypeId()).getIndustryTree()+subjectAccountTypeDTO.getTypeId()+"/");

        int a = industryDao.updateById(industry);
        //报错抛异常让aop回滚
        if(a != 1){
            throw new Exception("添加操作表失败失败");
        }
        return selectIndustryAccountType();
    }

    /**
     * 查询该父行业
     * @param industryId
     * @return
     */
    public Industry selectIndustryTree(Integer industryId){
        return industryDao.selectById(industryId);
    }

    /**
     * 删除industry表信息
     * @param industryId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json deleteIndustryById(Integer industryId,Integer userId) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        OperationRecord operationRecord = new OperationRecord();
        operationRecord.setOperationRecordRemarks("删除信息");
        operationRecord.setOperationRecordDescribe("删除信息");
        operationRecord.setOperationRecordTime(simpleDateFormat.format(new Date()));
        operationRecord.setUserId(userId);
        if(operationRecordService.insertOperationRecord(operationRecord) == null){
            throw new Exception("操作记录异常");
        }

        QueryWrapper<Industry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("industry_parent_id",industryId);
        long a = industryDao.selectCount(queryWrapper);
        if(a != 0){
            throw new Exception("靠，这个行业下还有子行业!!!");
        }
        int b = industryDao.deleteById(industryId);
        if(b != 1){
            throw new Exception("啧啧啧! 没删除成功!!!");
        }
        return selectIndustryAccountType();

    }

    /**
     * 添加一条行业类型信息
     * @param subjectAccountTypeDTO
     * @param userId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json insertIndustry(SubjectAccountTypeDTO subjectAccountTypeDTO,Integer userId) throws Exception {
        OperationRecord operationRecord = operationRecordService.operationRecordPublicMethod(userId,"添加行业信息",subjectAccountTypeDTO);
        if(operationRecord == null){
            throw  new Exception("操作信息加载失败");
            //System.out.println("");
            //return Json.fall("操作信息加载是啊比");
        }


        Industry industry = new Industry();
        if(subjectAccountTypeDTO.getTypeId() != 0){
            Industry industryParentId = industryDao.selectOne(new QueryWrapper<Industry>().eq("industry_id",subjectAccountTypeDTO.getTypeId()));
            System.out.println(industryParentId);
            if(industryParentId == null){
                throw  new Exception("父级信息未查到");
                //return Json.fall("父级信息未查到");
            }
            industry.setIndustryTree(industryParentId.getIndustryTree()+industryParentId.getIndustryId()+"/");
            System.out.println(industryParentId.getIndustryTree()+industryParentId.getIndustryId()+"/");
            industry.setLevel(industryParentId.getLevel() + 1);
        }else{
            industry.setIndustryTree("/");
            industry.setLevel(1);
        }
        industry.setIndustryParentId(subjectAccountTypeDTO.getTypeId());
        industry.setIndustryName(subjectAccountTypeDTO.getName());
        industry.setCreateOperationRecordId(operationRecord.getOperationRecordId());
        int a = industryDao.insert(industry);
        if(a != 1){
            throw new Exception("信息添加失败");
            //return Json.fall("信息添加失败");
        }
        System.out.println(industry);
        return selectIndustryAccountType();
    }


}
